// utils/Component/subject/GapFilling/GapFilling.js
var common = require('../../../common.js')
var dataUrl = require('../../../NetWork/sysJson.js')
var businessLogic = require('../../../NetWork/Model/MainbusinessLogic.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    rubric: "",
    daan: "",
    isSpeaking: 1,
    zhendaan:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //填空题的数据中心，这边表示答题的内容
    tiantimu(xiaoti, remarktype) {
      this.setData({
        daan: '',
        rubric: xiaoti,
        remarktype: remarktype,
      })
      console.log(this.data.rubric);
       if (remarktype == 4) {
        this.setData({
          daan: this.data.rubric.answer.split(",")
        })
        console.log("daan1",this.data.daan);
      } 
    },
    //开始录音
    Startrecord: function () {
      this.setData({
        isSpeaking: 2,
      })
      common.startRecord();
    },
    //停止录音
    Stoprecord: function (e) {
      this.setData({
        isSpeaking: 3,
      })
      this.stopRecord(e.currentTarget.dataset.id);
    },
    stopRecord: function (e) {
      console.log(e);
      const recorderManager = wx.getRecorderManager()
      const innerAudioContext = wx.createInnerAudioContext()
      recorderManager.stop(); 
      recorderManager.onStop((res) => {
        this.tempFilePath = res.tempFilePath;
        console.log('停止录音', res.tempFilePath)
        this.luyin(res.tempFilePath,e);
        const { tempFilePath } = res
      })
    },
    luyin:function(luyin,e){
      console.log(luyin,e);
      var that = this;
      wx.uploadFile({
        url: 'https://tizi.xingzi.org.cn/tizi/pub/admin/system/enclosure/uploadFileOne.do',
        filePath: luyin,
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },

        formData: {
          //和服务器约定的token, 一般也可以放在header中
          'session_token': wx.getStorageSync('session_token')
        },
        success: function (res) {
          // 这边就是获取录音的id 这样方便上传
          var data = JSON.parse(res.data);
          console.log(data);
          that.luyindata(data.result.id,e);
        }
      })
    },
   
    //播放录音
    Playrecord: function () {
      this.setData({
        isSpeaking: 1,
      })
      common.playRecord();
    },
    // 查看画图
    paint: function (e) {
      // console.log(e.currentTarget.dataset.id);
      wx.navigateTo({
        url: "../handInput/handInput?id=" + e.currentTarget.dataset.id + ""
      });

    },
    // 画图
    paintbase: function (e) {
      console.log(e);
      wx.navigateTo({
        url: "../lookhandInput/lookhandInput?id=" + e.currentTarget.dataset.id + ""
      });
    },
    //返回填空题的答案
    mydaan: function () {
      this.handInput();
      return this.data.zhendaan;
    },

    tktext(nexttex) {
      console.log("答案内容", nexttex)
      var nexttexobj = JSON.parse(nexttex);
      this.setData({
        zhendaan: nexttexobj,
        daan: nexttexobj,
      })
      console.log("daan2",this.data.daan)
      common.setStorage("base", this.data.daan)
    }, playMusic(num) {
      console.log("video", this.data.daan[num.currentTarget.dataset.id].data)
      //var src = this.data.daan[num.currentTarget.dataset.id].data
     
      let videoparams = { "fileid": this.data.daan[num.currentTarget.dataset.id].data }
      businessLogic.findFileInfo(videoparams, (res2) => {
        console.log(res2)
        if (res2.filepath.indexOf("upload") > -1) {
          var aaaa = res2.filepath.replace(/\\/g, "/");
          var src = dataUrl.dataUrl2 + "upload" + aaaa.split("upload")[1];
          console.log("wwwwwww1=" + src)
          common.playMusic(src);
        }
        })
    },
    //=============================获取答案
    
    //普通填空题
    bindKeyInput: function (e) {
      this.data.zhendaan[e.target.dataset.index] = e.detail.value
      this.setData({
        zhendaan: this.data.zhendaan
      })
      console.log(this.data.zhendaan);
    },
    //获取录音的答案
    luyindata: function (data, e) {
      var zhenti = [];
      var that = this;
      zhenti = that.data.zhendaan;
      zhenti[e] = data;
      that.setData({
        zhendaan: zhenti
      })
    },
    //手写填空题
    handInput:function(){
      let shouxie=common.getStorageSync("shouxie");

      var that = this;
      let zhenti = that.data.zhendaan;

      console.log(shouxie);
      if (shouxie == undefined || shouxie == null || shouxie == ""){
        
      } else{

        for (let a = 0; a < shouxie.length; a++) {
          if (shouxie[a] == null) {

          } else {
            zhenti[a] = shouxie[a];
          }
        }
        console.log(this.data.zhendaan);
        var that = this;
        that.setData({
          zhendaan: zhenti
        })

      }
      

    }

  }
})
